package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NLaboratoryFurnaceBo;
import com.ruoyi.zhgd.domain.vo.NLaboratoryFurnaceVo;
import com.ruoyi.zhgd.domain.NLaboratoryFurnace;
import com.ruoyi.zhgd.mapper.NLaboratoryFurnaceMapper;
import com.ruoyi.zhgd.service.INLaboratoryFurnaceService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 燃烧炉试验Service业务层处理
 *
 * @author zyb
 * @date 2023-09-11
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NLaboratoryFurnaceServiceImpl implements INLaboratoryFurnaceService {

    private final NLaboratoryFurnaceMapper baseMapper;

    /**
     * 查询燃烧炉试验
     */
    @Override
    public NLaboratoryFurnaceVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询燃烧炉试验列表
     */
    @Override
    public TableDataInfo<NLaboratoryFurnaceVo> queryPageList(NLaboratoryFurnaceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NLaboratoryFurnace> lqw = buildQueryWrapper(bo);
        Page<NLaboratoryFurnaceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询燃烧炉试验列表
     */
    @Override
    public List<NLaboratoryFurnaceVo> queryList(NLaboratoryFurnaceBo bo) {
        LambdaQueryWrapper<NLaboratoryFurnace> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NLaboratoryFurnace> buildQueryWrapper(NLaboratoryFurnaceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NLaboratoryFurnace> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBaseCode()), NLaboratoryFurnace::getBaseCode, bo.getBaseCode());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceCode()), NLaboratoryFurnace::getDeviceCode, bo.getDeviceCode());
        lqw.eq(StringUtils.isNotBlank(bo.getConstructionSite()), NLaboratoryFurnace::getConstructionSite, bo.getConstructionSite());
        lqw.like(StringUtils.isNotBlank(bo.getProjectName()), NLaboratoryFurnace::getProjectName, bo.getProjectName());
        lqw.eq(StringUtils.isNotBlank(bo.getSampleNo()), NLaboratoryFurnace::getSampleNo, bo.getSampleNo());
        lqw.like(StringUtils.isNotBlank(bo.getSampleName()), NLaboratoryFurnace::getSampleName, bo.getSampleName());
        lqw.eq(bo.getStartTime() != null, NLaboratoryFurnace::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, NLaboratoryFurnace::getEndTime, bo.getEndTime());
        lqw.eq(bo.getOriginalWeight1() != null, NLaboratoryFurnace::getOriginalWeight1, bo.getOriginalWeight1());
        lqw.eq(bo.getLostWeight1() != null, NLaboratoryFurnace::getLostWeight1, bo.getLostWeight1());
        lqw.eq(bo.getCompensationWeight1() != null, NLaboratoryFurnace::getCompensationWeight1, bo.getCompensationWeight1());
        lqw.eq(bo.getCompensationCoefficient1() != null, NLaboratoryFurnace::getCompensationCoefficient1, bo.getCompensationCoefficient1());
        lqw.eq(bo.getAsphaltContent1() != null, NLaboratoryFurnace::getAsphaltContent1, bo.getAsphaltContent1());
        lqw.eq(bo.getAsphaltAggregateRatio1() != null, NLaboratoryFurnace::getAsphaltAggregateRatio1, bo.getAsphaltAggregateRatio1());
        lqw.eq(bo.getMainTemperature1() != null, NLaboratoryFurnace::getMainTemperature1, bo.getMainTemperature1());
        lqw.eq(bo.getTestDuration1() != null, NLaboratoryFurnace::getTestDuration1, bo.getTestDuration1());
        lqw.eq(bo.getOriginalWeight2() != null, NLaboratoryFurnace::getOriginalWeight2, bo.getOriginalWeight2());
        lqw.eq(bo.getLostWeight2() != null, NLaboratoryFurnace::getLostWeight2, bo.getLostWeight2());
        lqw.eq(bo.getCompensationWeight2() != null, NLaboratoryFurnace::getCompensationWeight2, bo.getCompensationWeight2());
        lqw.eq(bo.getCompensationCoefficient2() != null, NLaboratoryFurnace::getCompensationCoefficient2, bo.getCompensationCoefficient2());
        lqw.eq(bo.getAsphaltContent2() != null, NLaboratoryFurnace::getAsphaltContent2, bo.getAsphaltContent2());
        lqw.eq(bo.getAsphaltAggregateRatio2() != null, NLaboratoryFurnace::getAsphaltAggregateRatio2, bo.getAsphaltAggregateRatio2());
        lqw.eq(bo.getMainTemperature2() != null, NLaboratoryFurnace::getMainTemperature2, bo.getMainTemperature2());
        lqw.eq(bo.getTestDuration2() != null, NLaboratoryFurnace::getTestDuration2, bo.getTestDuration2());
        lqw.eq(StringUtils.isNotBlank(bo.getIsQualified()), NLaboratoryFurnace::getIsQualified, bo.getIsQualified());
        lqw.eq(bo.getInsertTime() != null, NLaboratoryFurnace::getInsertTime, bo.getInsertTime());
        return lqw;
    }

    /**
     * 新增燃烧炉试验
     */
    @Override
    public Boolean insertByBo(NLaboratoryFurnaceBo bo) {
        NLaboratoryFurnace add = BeanUtil.toBean(bo, NLaboratoryFurnace.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改燃烧炉试验
     */
    @Override
    public Boolean updateByBo(NLaboratoryFurnaceBo bo) {
        NLaboratoryFurnace update = BeanUtil.toBean(bo, NLaboratoryFurnace.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NLaboratoryFurnace entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除燃烧炉试验
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
